﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using KPIS.GERP.Models;
using KPIS.GERP.Models.SYS;
using KPIS.GERP.Models.ViewModel;

namespace KPIS.GERP.WCF.Approval
{
    public static class ServerVariables
    {
        static ServerVariables()
        {
            InitialStartLog = new VMInitialStartLog();
        }

        public static VMInitialStartLog InitialStartLog { get; set; }
    }
    
    [ServiceContract]
    public interface IApprovalWakeupService
    {
        [OperationContract]
        void WakeUp();

        [OperationContract]
        VMInitialStartLog GetInitialTime();
    }
    public class ApprovalWakeupService : IApprovalWakeupService
    {
        public void WakeUp()
        {
            ServerVariables.InitialStartLog.Start();

            DateTime firstSelectDbStartDateTime = DateTime.Now;
            TimeSpan firstSelectDb, firstSaveDb;
            MockQuerySelect();
            firstSelectDb = DateTime.Now.Subtract(firstSelectDbStartDateTime);

            DateTime firstSaveDbStartDateTime = DateTime.Now;
            var m = MockQuerySave();
            firstSaveDb = DateTime.Now.Subtract(firstSaveDbStartDateTime);

            m.Description = "First database select time used = " + firstSelectDb.TotalSeconds.ToString("#,##0") + " seconds , save time used = " + firstSaveDb.TotalSeconds.ToString("#,##0") + " seconds. Remarks: this message is generated by System";

            MockQueryUpdate(m);
            ServerVariables.InitialStartLog.FinishedInitial();

        }

        public VMInitialStartLog GetInitialTime()
        {
            return ServerVariables.InitialStartLog;
        }

        private void MockQueryUpdate(Message newMessage)
        {
            using (var db = new GERPContext(false))
            {
                db.Entry(newMessage).State = EntityState.Modified;
                if (Models.SYS.DevConfig.SendMessageServersStartupToAdmin) db.SaveChanges();
            }
        }

        private Message MockQuerySave()
        {
            using (var db = new GERPContext(false))
            {
                var newMessage = new Message
                    {
                        Code = "ESS Wake up",
                        Date = DateTime.Now,
                        Title = "Server service ESS has been invoke and test call database",
                        ToEmpId = 1,
                        MessageStatus = MessageWarningStatus.Warn,
                        ActionCount = 0,
                        RuleApprove = 1,
                        ApprovalTask = 1,
                        RecordStatus = RecordStatus.UsedStatus
                    };

                db.Messages.Add(newMessage);
                if(Models.SYS.DevConfig.SendMessageServersStartupToAdmin) db.SaveChanges();
                return newMessage;
            }
        }

        private void MockQuerySelect()
        {
            using (var db = new GERPContext(false))
            {
                var sysMessage = db.Messages.ToList();
            }
        }
    }
}

}
